home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part1 / 6054 < prev    next >
Encoding:
Text File  |  1996-08-05  |  967 b   |  31 lines

  1. Newsgroups: comp.lang.c
  2. Path: link-1.ts.bcc.ac.uk!reavpar
  3. From: reavpar@ucl.ac.uk (Mr Philip Arthur Riebold)
  4. Subject: Re: How to get a random string each time??
  5. Message-ID: <1996Feb22.123954.80675@ucl.ac.uk>
  6. Date: Thu, 22 Feb 1996 12:39:54 GMT
  7. References: <4fh5od$qq0@news.nevada.edu> <4g4c5m$tlb@news.nevada.edu> <1996Feb21.150754.81848@ucl.ac.uk>
  8. Organization: University College London
  9.  
  10. reavpar@ucl.ac.uk (Mr Philip Arthur Riebold) writes:
  11.  
  12. >int main(void)
  13. >{
  14. >    char line[BUFSIZ], it[BUFSIZ];
  15. >    int n;
  16. >    srand48(time(NULL)); /* or seed the generator in some other way */
  17. >    for(n = 1; gets(line); n++) /*probably better to use fgets()*/
  18. >        if((n * drand48()) < 1.0) /*drand returns a double in [0.0, 1.0) */
  19.  
  20. Oops !, sorry I made a typo, the above line should (of course !) be
  21.          if(drand48() < (1.0/n))
  22.  
  23. >            strcpy(it, line);
  24. >    puts(it);
  25. >}
  26.  
  27. BTW thinking about it the code really _should_ have used fgets()
  28. --
  29.     Philip Riebold,
  30.  
  31.